<?php

namespace App\Admin\Controllers;

use App\Models\BlindBoxCategory;
use App\Models\Category;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use Encore\Admin\Controllers\AdminController;

class BlindBoxCategoriesController extends AdminController
{
    protected $title = '盲盒分类';
    
    protected function grid()
    {
        $grid = new Grid(new BlindBoxCategory());
	
	    $grid->disableExport();
	    
	    $grid->column('name', '盲盒分类名称');
	    $grid->column('is_show', '显示')->bool();
	    $grid->column('created_at', '创建时间')->display(function () {
	    	return $this->created_at->toDatetimeString();
	    });
	    $grid->column('order', '排序')->editable();
        return $grid;
    }
    
    protected function detail($id)
    {
        $show = new Show(BlindBoxCategory::findOrFail($id));



        return $show;
    }
    
    protected function form()
    {
        $form = new Form(new BlindBoxCategory());
	   	$this->formCategory($form);
	    $form->text('name', '盲盒分类名称');
	    $form->radio('is_show', '显示')->default(true)->options(config('site.bool'));
	    $form->number('order', '排序')->min(1)->default(1);

        return $form;
    }

    protected function formCategory(Form $form)
    {
	    $form->select('category_id', '所属分类')
		    ->options(Category::selectOptions())
		    ->rules(['nullable', function ($attribute, $value, $fail) {
			    
			    return true;
		    }]);
    }
}
